<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <style>
        .demonstration {
            display: block;
            color: #8492a6;
            font-size: 14px;
            margin-top: 20px;
        }
    </style>
</head>
<body>

<div id="app">
    <h3>基础用法</h3>
    <div>
        <el-tree :data="data" :props="defaultProps" @node-click="handleNodeClick"></el-tree>
    </div>


    <h3>可选择</h3>
    <div style="margin: 20px 0">
        <el-tree :data="data" show-checkbox :props="defaultProps" @node-click="handleNodeClick"></el-tree>
    </div>

</div>

<h3>节点过滤</h3>
<div style="margin: 20px 0"  id="tree">
    <el-input style="width:200px"
            placeholder="输入关键字进行过滤"
            v-model="filterText">
    </el-input>

    <el-tree
            class="filter-tree"
            :data="data2"
            :props="defaultProps"
            default-expand-all
            :filter-node-method="filterNode"
            show-checkbox
            ref="tree2">
    </el-tree>


</div>

</body>
<!-- 先引入 Vue -->
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<!-- 引入组件库 -->
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
<script>
    new Vue({
        el: '#app',
        watch: {
            filterText: function (val) {
                this.$refs.tree2.filter(val);
            }
        },

        methods: {
            filterNode: function (value, data) {
                if (!value) return true;
                return data.label.indexOf(value) !== -1;
            }
        },
        data: {
            filterText: '',
            data: [{
                label: '一级 1',
                children: [{
                    label: '二级 1-1',
                    children: [{
                        label: '三级 1-1-1'
                    }]
                }]
            }, {
                label: '一级 2',
                children: [{
                    label: '二级 2-1',
                    children: [{
                        label: '三级 2-1-1'
                    }]
                }, {
                    label: '二级 2-2',
                    children: [{
                        label: '三级 2-2-1'
                    }]
                }]
            }, {
                label: '一级 3',
                children: [{
                    label: '二级 3-1',
                    children: [{
                        label: '三级 3-1-1'
                    }]
                }, {
                    label: '二级 3-2',
                    children: [{
                        label: '三级 3-2-1'
                    }]
                }]
            }],
            defaultProps: {
                children: 'children',
                label: 'label'
            }
        }

    });

    new Vue({
        el: '#tree',
        watch: {
            filterText:function(val) {
                this.$refs.tree2.filter(val);
            }
        },

        methods: {
            filterNode:function(value, data) {
                if (!value) return true;
                return data.label.indexOf(value) !== -1;
            }
        },

        data: {
            filterText: '',
            data2: [
                {
                    id: 1,
                    label: '一级 1',
                    children: [{
                        id: 4,
                        label: '二级 1-1',
                        children: [{
                            id: 9,
                            label: '三级 1-1-1'
                        }, {
                            id: 10,
                            label: '三级 1-1-2'
                        }]
                    }]
                }, {
                    id: 2,
                    label: '一级 2',
                    children: [{
                        id: 5,
                        label: '二级 2-1'
                    }, {
                        id: 6,
                        label: '二级 2-2'
                    }]
                },
                {
                    id: 3,
                    label: '一级 3',
                    children: [{
                        id: 7,
                        label: '二级 3-1'
                    }, {
                        id: 8,
                        label: '二级 3-2'
                    }]
                }],
            defaultProps: {
                children: 'children',
                label: 'label'
            }

        }

    })

</script>
</html>